import axios from 'axios';
import baseUrl from './globalUrl.js';
// create an axios instance
const service = axios.create({
  baseURL: baseUrl.globalUrl,
  timeout: 50000000000
});
// 拦截器
service.interceptors.request.use(
  config => {
    // 在发送请求之前设置头部
    let token = '';
    uni.getStorage({
      key: 'token',
      success: (res) => {
        token = res.data;
      }
    });

    if (token) {
      config.headers['Token'] = token;
    }
    return config;
  },
  error => {
    // 输出错误信息
    console.log(error); // for debug
    return Promise.reject(error);
  }
);
// 拦截器
service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.success) {
      if (res.code === 10003) {
        uni.showToast({
          mask: true,
          title: '登陆超时',
          duration: 2000,
          icon: 'none'
        });
        // 清除缓存
        localStorage.clear();
        setTimeout(() => {
          //	window.location.reload()
        }, 500);
        // eslint-disable-next-line no-empty
      } else {}
    } else {
      // if (res.code === 10003) {
      // 	uni.showToast({
      // 		mask: true,
      // 		title: '登陆超时',
      // 		duration: 2000,
      // 		icon: "none"
      // 	});
      // 	// 清除缓存
      // 	localStorage.clear()
      // 	setTimeout(() => {
      // 		window.location.reload()
      // 	}, 500)
      // }

    }
    return res;
  },
  error => {
    console.log('err' + error); // for debug
    uni.showToast({
      mask: true,
      title: '暂无数据 ╮(╯▽╰)╭',
      duration: 2000,
      icon: 'none'
    });
    // window.location.reload()
    return Promise.reject(error);
  }
);
export default service;
